package controller

import (
	"Graphite/model"
	"Graphite/service"
	"Graphite/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"time"
)

//var err error
//添加配置表记录

func AddCon(c *gin.Context) {
	var pz model.PeiZhi
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&pz); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(pz); err == nil {
			pz.CreatedAt = formatNow
			col := "configuration"
			id := util.AddBasic(pz, col)
			data := service.ResultCon(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//浸渍1
//添加浸渍小时数据

func AddHourData(c *gin.Context) {
	var dhd model.DippingHourData
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&dhd); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(dhd); err == nil {
			dhd.CreatedAt = formatNow
			col := "dippinghourdata"
			id := util.AddBasic(dhd, col)
			data := service.ResultHour(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//添加天然气用量

func AddGasUsage(c *gin.Context) {
	var ngu model.NaturalGasUsage
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&ngu); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(ngu); err == nil {
			ngu.CreatedAt = formatNow
			col := "naturalgasusage"
			id := util.AddBasic(ngu, col)
			data := service.ResultGas(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//浸渍2
//浸渍系统运行记录

func AddDippingNote(c *gin.Context) {
	var dsn model.DippingSystemNote
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&dsn); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(dsn); err == nil {
			fmt.Println(dsn)
			dsn.CreatedAt = formatNow
			col := "dippingsystemnote"
			var i int64
			for i = 0; i <= 5; i++ { //手动生成数组里objectId
				id := primitive.NewObjectID()
				dsn.Case[i].Id = id
			}
			_id := util.AddBasic(dsn, col)
			data := service.ResultDipNote(_id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//石墨化1
//用户质量要求

func AddQuality(c *gin.Context) {
	var qr model.QualityRequired
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&qr); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(qr); err == nil {
			qr.CreatedAt = formatNow
			col := "qualityrequired"
			id := util.AddBasic(qr, col)
			data := service.ResultQua(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//装炉报告

func AddLoadOven(c *gin.Context) {
	var lo model.LoadOven
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&lo); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(lo); err == nil {
			lo.CreatedAt = formatNow
			col := "loadoven"
			id := util.AddBasic(lo, col)
			data := service.ResultLoad(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//送电曲线

func AddEleCurve(c *gin.Context) {
	var ec model.ElectricityCurve
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&ec); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(ec); err == nil {

			ec.CreatedAt = formatNow
			col := "electricitycurve"
			id := util.AddBasic(ec, col)
			data := service.ResultEle(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//石墨化2

func AddBasic(c *gin.Context) {
	var bi model.BasicInformation
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&bi); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(bi); err == nil {

			bi.CreatedAt = formatNow
			col := "basicinformation"
			id := util.AddBasic(bi, col)
			data := service.ResultBasic(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//详细信息

func AddDetail(c *gin.Context) {
	var det model.DetailedInformation
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&det); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(det); err == nil {
			fmt.Printf("--------:%v", det)
			det.CreatedAt = formatNow
			col := "detailedinformation"
			id := util.AddBasic(det, col)
			data := service.ResultDetail(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//成型1
//筛分记录

func AddScreening(c *gin.Context) {
	var sr model.ScreeningRecord
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&sr); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(sr); err == nil {

			//fmt.Println("payload：",sr)
			////i:=0
			sr.CreatedAt = formatNow
			col := "screeningrecord"
			id := util.AddBasic(sr, col)
			data := service.ResultScr(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//成型2
//配料记录

func AddIngredient(c *gin.Context) {
	var ir model.IngredientRecord
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&ir); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(ir); err == nil {
			ir.CreatedAt = formatNow
			col := "ingredientrecord"
			id := util.AddBasic(ir, col)
			data := service.ResultIngredient(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//库管
//原始记录

func AddInitRecord(c *gin.Context) {
	var ir model.InitRecord
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&ir); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(ir); err == nil {

			//fmt.Println("payload：",ir)
			////i:=0
			ir.CreatedAt = formatNow
			col := "initrecord"
			id := util.AddBasic(ir, col)
			data := service.ResultInit(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//质检
//检查日报

func AddDailyReport(c *gin.Context) {
	var dr model.DailyReport
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&dr); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(dr); err == nil {

			//fmt.Println("payload：",dr)
			////i:=0
			dr.CreatedAt = formatNow
			col := "dailyreport"
			id := util.AddBasic(dr, col)
			data := service.ResultDaily(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//产品信息

func AddProduction(c *gin.Context) {
	var pro model.Production
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&pro); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(pro); err == nil {

			//fmt.Println("payload：",dr)
			////i:=0
			pro.CreatedAt = formatNow
			col := "production"
			id := util.AddBasic(pro, col)
			data := service.ResultPro(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//隧道窑
//测点信息
func AddLocation(c *gin.Context) {
	var loc model.LocationInfo
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&loc); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(loc); err == nil {

			//fmt.Println("payload：",dr)
			////i:=0
			loc.CreatedAt = formatNow
			col := "locationinformation"
			id := util.AddBasic(loc, col)
			data := service.ResultLoc(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//进车信息

func AddIncar(c *gin.Context) {
	var in model.Incar_Info
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&in); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(in); err == nil {

			//fmt.Println("payload：",dr)
			////i:=0
			in.CreatedAt = formatNow
			col := "incar"
			id := util.AddBasic(in, col)
			data := service.ResultIncar(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//出车

func AddOutcar(c *gin.Context) {
	var out model.Outcar_Info
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&out); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(out); err == nil {

			//fmt.Println("payload：",dr)
			////i:=0
			out.CreatedAt = formatNow
			col := "outcar"
			id := util.AddBasic(out, col)
			data := service.ResultOutcar(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

//交接班
func AddHan_Suc(c *gin.Context) {
	var hs model.Handover_Successor
	now := time.Now()
	formatNow := now.Format("2006-01-02 15:04:05")
	if err := c.ShouldBind(&hs); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		if err := util.CheckData(hs); err == nil {

			//fmt.Println("payload：",dr)
			////i:=0
			hs.CreatedAt = formatNow
			col := "han_suc"
			id := util.AddBasic(hs, col)
			data := service.ResultHan_Suc(id, col)
			util.Return(c, 200, util.SUCCESS, data)
		} else {
			fmt.Println("add err:")
			util.Return(c, 200, util.PARA_ERROR, err)
		}
	}
}

func AddPlat_Para(c *gin.Context) {
	var plat model.PlatformParameters
	if err := c.Bind(&plat); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		util.CreateOne(c, "platformparameters", plat, "_id", plat.Id.Hex())
		return
	}
}
/*货物管理
数量表*/
func AddAmount(c *gin.Context){
	one,_:=c.Get("one")
	amount :=one.(model.Amount)
	//fmt.Println(amount)
		if err := util.CheckData(amount);err == nil {
			col := "amount"
			col2 := "weight"
			fmt.Println(amount)
			id := util.AddBasic(amount, col)
			data := service.ResultAmount(id, col)
			switch amount.Diameter {
			case "Φ600":
				id2 := service.AddWeight600(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ550":
				id2 := service.AddWeight550(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ500":
				id2 := service.AddWeight500(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ450":
				id2 := service.AddWeight450(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ400":
				id2 := service.AddWeight400(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ350":
				id2 := service.AddWeight350(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ300":
				id2 := service.AddWeight300(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ250":
				id2 := service.AddWeight250(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ200":
				id2 := service.AddWeight200(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ234":
				id2 := service.AddWeight234(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ256":
				id2 := service.AddWeight256(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ288":
				id2 := service.AddWeight288(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ315":
				id2 := service.AddWeight315(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "Φ333":
				id2 := service.AddWeight333(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			case "次品":
				id2 := service.AddDefective(c,amount,col2)
				fmt.Println(id2)
				util.Return(c,200,util.SUCCESS,data)
			}
		}else {
			util.Return(c,200,util.PARA_ERROR,err)
		}
}

//货物汇总表


func CargoSummaryInsert(c *gin.Context) {
	var cargo model.CargoSummary
	if err := c.Bind(&cargo); err != nil {
		util.Return(c, 200, util.PARA_ERROR, err)
		return
	} else {
		util.CreateOne(c, "CargoSummary", cargo, "_id", cargo.Id.Hex())
		return
	}
}

